/**
 *  编辑页
 *
 * 作者 admin
 * 日期 2024-08-07 16:43:01
 */
layui.use(['jquery','febs', 'form','laydate', 'validate','fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        laydate = layui.laydate,
        form = layui.form,
        table = layui.table,
        fileUpload = layui.fileUpload,
        $view = $('#reportDetail-edit'),
        $lessonView = $('#baseCautionaryLesson-edit'),
        $accidentType = $('div[name="accidentType"]'),
        $accidentLevel = $('div[name="accidentLevel"]'),
        $jobOperation = $('div[name="jobOperation"]'),
        injuryUserTableIns,
        validate = layui.validate;
    let injuryUserTableData = [];
    let accidentReportFileList = [];
    let accidentSurveyFileList = [];
    var fileList = [];
    var tenantId = POPUP_DATA.tenantId;

    initDictItemLabel_reportDetail();
    form.verify(validate);
    form.render();
    initFormValue();
    initFormLessonValue();
    initInjuryUserTable();

    laydate.render({
        elem: "#accidentDate",
        done: function (value, date) {},
        type: 'datetime'
    })

    laydate.render({
        elem: "#surveyDeadLine",
        done: function (value, date) {},
        type: 'date'
    })

    laydate.render({
        elem: "#noticeDeptDate",
        done: function (value, date) {},
    })
    function initDictItemLabel_reportDetail(){
        febs.initDictItemLabel($accidentType,'SAFETY_ACCIDENT_TYPE',accidentReportData.accidentType);
        febs.initDictItemLabel($accidentLevel,'SAFETY_ACCIDENT_LEVEL',accidentReportData.accidentLevel);
        febs.initDictItemLabel($jobOperation,'SAFETY_JOB_OPERATION',accidentReportData.jobOperation);
    }
    function initFormValue() {
        if (accidentReportData) {
            //伤亡人员
            injuryUserTableData = accidentReportData.injuryUserDataList;
            //事故上报附件
            accidentReportFileList = accidentReportData.accidentReportFileList;
            if(accidentReportFileList && accidentReportFileList.length>0){
                renderFile('#uploadReportBtn',accidentReportFileList);
            }
            //事故调查附件
            accidentSurveyFileList = accidentReportData.accidentSurveyFileList;
            if(accidentSurveyFileList && accidentSurveyFileList.length>0){
                renderFile('#uploadReportSurveyBtn',accidentSurveyFileList);
            }
            form.val("reportSurveyAudit-edit-form", {
                "reportId": accidentReportData.reportId,
                "tenantId": accidentReportData.tenantId,
                "accidentNo": accidentReportData.accidentNo,
                "accidentType": accidentReportData.accidentType,
                "accidentTitle": accidentReportData.accidentTitle,
                "accidentDate": accidentReportData.accidentDate,
                "accidentAreaId": accidentReportData.accidentAreaId,
                "accidentAreaName": accidentReportData.accidentAreaName,
                "accidentLevel": accidentReportData.accidentLevel,
                "accidentReason": accidentReportData.accidentReason,
                "noticeDeptFlg": accidentReportData.noticeDeptFlg,
                "noticeDeptName": accidentReportData.noticeDeptName,
                "noticeDeptDate": accidentReportData.noticeDeptDate,
                "preliminarilyDamage": accidentReportData.preliminarilyDamage,
                "preliminarilyDescript": accidentReportData.preliminarilyDescript,
                "status": accidentReportData.status,
                "surveyStatus": accidentReportData.surveyStatus,
                "surveyDeadline": accidentReportData.surveyDeadline,
                "surveyDeptId": accidentReportData.surveyDeptId,
                "surveyDeptName": accidentReportData.surveyDeptName,
                "surveyUserId": accidentReportData.surveyUserId,
                "surveyUserName": accidentReportData.surveyUserName,
                "surveyRealName": accidentReportData.surveyRealName,
                "surveyRemark": accidentReportData.surveyRemark,
                "accidentElapse": accidentReportData.accidentElapse,
                "deathToll": accidentReportData.deathToll,
                "severeInjuryToll": accidentReportData.severeInjuryToll,
                "slightInjuryToll": accidentReportData.slightInjuryToll,
                "missingToll": accidentReportData.missingToll,
                "economyLoss": accidentReportData.economyLoss,
                "accidentOverview": accidentReportData.accidentOverview,
                "directReason": accidentReportData.directReason,
                "indirectionReason": accidentReportData.indirectionReason,
                "leaderUserId": accidentReportData.leaderUserId,
                "leaderUserName": accidentReportData.leaderUserName,
                "leaderUserNo": accidentReportData.leaderUserNo,
                "leaderRealName": accidentReportData.leaderRealName,
                "leaderDeptId": accidentReportData.leaderDeptId,
                "leaderDeptName": accidentReportData.leaderDeptName,
                "obligationRatio": accidentReportData.obligationRatio,
                "obligationCognizance": accidentReportData.obligationCognizance,
                "leaderDisposeCondition": accidentReportData.leaderDisposeCondition,
                "surveyAuditUserId": accidentReportData.surveyAuditUserId,
                "surveyAuditRealName": accidentReportData.surveyAuditRealName,
                "surveyAuditDate": accidentReportData.surveyAuditDate,
                "surveyAuditRemark": accidentReportData.surveyAuditRemark,
                "rectificationStatus": accidentReportData.rectificationStatus,
                "rectificationDeadline": accidentReportData.rectificationDeadline,
                "rectificationDeptId": accidentReportData.rectificationDeptId,
                "rectificationDeptName": accidentReportData.rectificationDeptName,
                "rectificationUserId": accidentReportData.rectificationUserId,
                "rectificationUserName": accidentReportData.rectificationUserName,
                "rectificationRealName": accidentReportData.rectificationRealName,
                "rectificationRemark": accidentReportData.rectificationRemark,
                "rectificationDate": accidentReportData.rectificationDate,
                "rectificationMeasures": accidentReportData.rectificationMeasures,
                "educationCondition": accidentReportData.educationCondition,
                "rectificationAuditUserId": accidentReportData.rectificationAuditUserId,
                "rectificationAuditRealName": accidentReportData.rectificationAuditRealName,
                "rectificationAuditDate": accidentReportData.rectificationAuditDate,
                "rectificationAuditRemark": accidentReportData.rectificationAuditRemark,
                "noticeChic": accidentReportData.noticeChic,
                "noticeDate": accidentReportData.noticeDate,
            });
            if(accidentReportData.noticeDeptFlg === '1'){
                $("#noticeDeptDiv").show();
            }else{
                $("#noticeDeptDiv").hide();
            }
            if(accidentReportData.casualtyRemark === '1'){
                $("#casualtyRemarkDiv").show();
            }else{
                $("#casualtyRemarkDiv").hide();
            }
            //调查原因拼接html
            if (accidentReportData.reportReasonList) {
                let reasonList = accidentReportData.reportReasonList;
                for(let i= 0;i<reasonList.length;i++){
                    appendReportReason(i,reasonList[i]);
                }
            }
            form.render('checkbox');
        }
    }

    function appendReportReason(i,item){
        var groupPanelAppend = '<div class="layui-panel" id="panel_reason0" >' +
            '                                            <div class="layui-field-box" name="reasonBox">' +
            '                                                <div class="layui-form-item" >' +
            '                                                    <div class="layui-row">' +
            '                                                        <div class="layui-col-xs6">' +
            '                                                            <label class="layui-form-label layui-form-label-detail ">调查时间：</label>' +
            '                                                            <div class="layui-input-block layui-input-block-html" >'+item.surveyDate+'' +
            '                                                            </div>' +
            '                                                        </div>' +
            '                                                        <div class="layui-col-xs6">' +
            '                                                            <label class="layui-form-label layui-form-label-detail">原因分类：</label>' +
            '                                                            <div class="layui-input-block layui-input-block-html" name="reasonType'+i+'">' +
            '                                                            </div>' +
            '                                                        </div>' +
            '                                                    </div>' +
            '                                                </div>' +
            '                                                <div class="layui-form-item" >' +
            '                                                    <div class="layui-row">' +
            '                                                        <div class="layui-col-xs6">' +
            '                                                            <label class="layui-form-label layui-form-label-detail">直接原因：</label>' +
            '                                                            <div class="layui-input-block layui-input-block-html" >'+item.directReason+'' +
            '                                                            </div>' +
            '                                                        </div>' +
            '                                                        <div class="layui-col-xs6">' +
            '                                                            <label class="layui-form-label layui-form-label-detail">间接原因：</label>' +
            '                                                            <div class="layui-input-block layui-input-block-html" >'+item.indirectionReason+'' +
            '                                                            </div>' +
            '                                                        </div>' +
            '                                                    </div>' +
            '                                                </div>' +
            '                                                <div class="layui-form-item">' +
            '                                                    <div class="layui-row">' +
            '                                                        <div class="layui-col-xs12">' +
            '                                                            <label class="layui-form-label layui-form-label-detail">原因概述：</label>' +
            '                                                            <div class="layui-input-block layui-input-block-html" >'+item.reasonContent+'' +
            '                                                            </div>' +
            '                                                        </div>' +
            '                                                    </div>' +
            '                                                </div>' +
            '                                                <div class="layui-form-item">' +
            '                                                    <div class="layui-row">' +
            '                                                        <div class="layui-col-xs4">' +
            '                                                            <label class="layui-form-label layui-form-label-detail">是否整改：</label>' +
            '                                                            <div class="layui-input-block layui-input-block-html" >'+(item.rectificationFlg=="1"?"是":"否")+'' +
            '                                                            </div>' +
            '                                                        </div>' +
            '                                                        <div class="febs-hide" name="ifShow'+i+'">' +
            '                                                            <div class="layui-col-xs4" >' +
            '                                                                <label class="layui-form-label layui-form-label-detail">整改人：</label>' +
            '                                                                <div class="layui-input-block layui-input-block-html" >'+item.rectificationUsers+'' +
            '                                                                </div>' +
            '                                                            </div>' +
            '                                                            <div class="layui-col-xs4" >' +
            '                                                                <label class="layui-form-label layui-form-label-detail">整改期限：</label>' +
            '                                                                <div class="layui-input-block layui-input-block-html" >'+item.rectificationDeadline+'' +
            '                                                                </div>' +
            '                                                            </div>' +
            '                                                        </div>' +
            '                                                    </div>' +
            '                                                </div>' +
            '                                                <div class="layui-form-item febs-hide" name="ifShow'+i+'">' +
            '                                                    <div class="layui-row">' +
            '                                                        <div class="layui-col-xs12">' +
            '                                                            <label class="layui-form-label layui-form-label-detail">整改要求：</label>' +
            '                                                                <div class="layui-input-block layui-input-block-html" >'+item.rectificationOpinion+'' +
            '                                                                </div>' +
            '                                                        </div>' +
            '                                                    </div>' +
            '                                                </div>' +
        '                                                    <div class="layui-form-item">' +
        '                                                        <div class="layui-col-xs12">' +
        '                                                            <label class="layui-form-label layui-form-label-detail">原因附件：</label>' +
        '                                                            <div class="layui-input-block layui-input-block-html" id="uploadReportReasonBtn'+i+'">' +
        '                                                            </div>' +
        '                                                        </div>' +
        '                                                    </div>' +
            '                                            </div>' +
            '                                        </div>'
        ;

        $("#reason_box").append(groupPanelAppend);
        if(item.rectificationFlg=="1"){
            $('div[name="ifShow'+i+'"]').show();
        }
        febs.initDictItemLabel($('div[name="reasonType'+i+'"]'),'SAFETY_ACCIDENT_REASON',item.reasonType);
        //渲染事故原因附件
        renderFile('#uploadReportReasonBtn'+i+'',item && item.accidentReasonSurveyFileList ? item.accidentReasonSurveyFileList:[]);
        form.render();
    }

    function renderFile(elem,fileList){
        fileUpload.render({
            elem:elem,
            url:ctx + 'accident/upload',
            type:'2',
            maxNum:5,
            isDetail:true,
            defaultData: fileList,
            done:function(res){
            }
            ,error: function(){
                layer.msg("服务器异常")
            }
        })
    }

    function initInjuryUserTable() {
        injuryUserTableIns = febs.table.init({
            elem: $view.find('#injuryUserTable'),
            id: 'injuryUserTable',
            page:false,
            skin: '',
            limit:Number.MAX_VALUE,
            editTrigger: 'dblclick',
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'userId', title: 'id',hide:true},
                {field: 'userName', title: 'userName',hide:true},
                {field: 'userNo', title: 'userNo',hide:true},
                {field: 'deptId', title: 'deptId',hide:true},
                {field: 'realName',align:'center', title: '姓名', minWidth: 100},
                {field: 'sex',align:'center', title: '性别',width: 60, templet: function (d) {
                        var flg = '';
                        switch (d.sex) {
                            case "0": {
                                flg = '<span>男</span>'
                                break;
                            }
                            case "1": {
                                flg = '<span>女</span>'
                                break;
                            }
                            case "2": {
                                flg = '<span>保密</span>'
                                break;
                            }
                        }
                        return flg;
                    }
                },
                {field: 'deptName',align:'center', title: '部门', minWidth: 150},
                {field: 'userType',align:'center', title: '用工形式', minWidth: 100, templet: function (d) {
                        var flg = '';
                        switch (d.userType) {
                            case "1": {
                                flg = '<span>本工</span>'
                                break;
                            }
                            case "2": {
                                flg = '<span>外包工</span>'
                                break;
                            }
                            case "3": {
                                flg = '<span>外协工</span>'
                                break;
                            }
                        }
                        return flg;
                    }
                },
                {field: 'userSeniority',align:'center', title: '工龄(年)', minWidth: 80},
                {field: 'userDegree',align:'center', title: '学历', minWidth: 80},
                {field: 'identificationNo',align:'center', title: '身份证号', minWidth: 240},
                {field: 'injuryLevel',align:'center',title: '伤害程度',minWidth: 160,templet:function (d){
                        if(d.injuryLevel){
                            return febs.getDictLabel(injuryLevelList,d.injuryLevel)
                        }
                    }
                },
                {field: 'economyLoss',align:'center', title: '经济损失(元)', minWidth: 120}
            ]],
            done: function(res, curr, count){
            },
            data:injuryUserTableData
        });
    }

    function initFormLessonValue() {
        if (baseCautionaryLessonData) {
            form.val("baseCautionaryLesson-edit-form", {
                "cautionaryLessonId": baseCautionaryLessonData.cautionaryLessonId,
                "tenantId": baseCautionaryLessonData.tenantId,
                "cautionarySource": baseCautionaryLessonData.cautionarySource,
                "cautionarySourceId": baseCautionaryLessonData.cautionarySourceId,
                "cautionaryTitle": baseCautionaryLessonData.cautionaryTitle,
                "cautionaryDeptId": baseCautionaryLessonData.cautionaryDeptId,
                "cautionaryDept": baseCautionaryLessonData.cautionaryDept,
                "studyDate": baseCautionaryLessonData.studyDate,
                "studyRemark": baseCautionaryLessonData.studyRemark,
                "status": baseCautionaryLessonData.status,
            });
            if(baseCautionaryLessonData.uploadFileInfo != null){
                fileList.push(baseCautionaryLessonData.uploadFileInfo);
            }
        }
    }

    fileUpload.render({
        elem:'#uploadBtn',
        url:ctx + 'base/upload',
        type:'2',
        maxNum:1,
        defaultData: fileList.length>0 ? fileList:[],
        done:function(res){
        }
        ,error: function(){
            layer.msg("服务器异常")
        }
    })

    form.on('submit(baseCautionaryLesson-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        data.uploadFileInfo = getFileData();
        console.log(data);
        febs.modal.confirm("保存提示！","是否保存？",function (index){
            if (data.cautionaryLessonId && data.cautionaryLessonId.length > 0) {
                febs.postArray(ctx + 'baseCautionaryLesson/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-baseCautionaryLesson').find('#query').trigger('click');
                });
            } else {
                febs.postArray(ctx + 'baseCautionaryLesson', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-baseCautionaryLesson').find('#query').trigger('click');
                });
            }
        })
    }

    function getFileData(){
        var fileDatas = $lessonView.find(".fileOne")
        var fileList = [];
        $.each(fileDatas,function (index, item){
            var data = {
                filePath: item.dataset.filepath,
                fileName: item.dataset.filename,
                smallPath: item.dataset.smallpath,
            }
            fileList.push(data);
        })
        return fileList[0];
    }

});